// =============================================================
// CFactory/CService/CServiceProxy/CServiceContainer
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Aug. 24, 2007
// =============================================================
#include "CServiceContainer.h"

CServiceContainer::CServiceContainer()
{
	factory = new CFactoryService();
}

CServiceContainer::~CServiceContainer()
{
	delete factory;
}

int CServiceContainer::plugin(CService *service)
{
	// activate must be overriden
	activate(service);

	services.push_back(service);

	return 1;
}

int CServiceContainer::plugout(char *name)
{
	vector<CService *>::iterator service = services.begin();
	while(service!=services.end()) {
		char *sname = (*service)->getName();
		int same = strcmp(sname,name);

		cout << "-----------------------------------------------------" << endl;
		cout << "### you must also use instance name to match services" << endl;
		cout << "-----------------------------------------------------" << endl;

		if(same) {
			// inactivate must be overriden
			inactivate((*service));

			delete (*service);
			services.erase(service);

			return 1;
		}

		service++;
	}

	return 0;
}

CService *CServiceContainer::getService(char *name)
{
	vector<CService *>::iterator service = services.begin();
	while(service!=services.end()) {
		char *sname = (*service)->getName();
		int same = strcmp(sname,name);

		if(same==0) return *service;

		service++;
	}

	return NULL;
}

// this will be implemented in CSimpleContainer
void CServiceContainer::runCGI()
{
	factory->serve(); // as cgi server
}

// this will be implemented in CSimpleContainer
void CServiceContainer::run()
{
	factory->serve(); // as stand-alone server
}
